/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bagtest;

import java.util.Collections;

/**
 *
 * @author haoliang.donghl
 */
public class GreedyAlgorithm extends AbstractAlgorithm{

    @Override
    public String getName() {
        return "Greedy Algorithm";
    }

    @Override
    public void doCalculate() {
        float remain = this.capacity;
        for (Item item : items){
            item.setDensity(item.getValue() / item.getSize());
        }
        Collections.sort(items);
        for (int i = 0; i < items.size(); i++){
            Item item = items.get(i);
            if (remain > item.getSize()){
                inBag.add(item);
                remain -= item.getSize();
            }else{
                break;
            }
        }
    }

}
